/* implement a queue data structure with linked list*/

var Node = require('./node');

class Queue {
    constructor() {
        this._first = null;
        this._last = null;
        this._count = 0;
    }

    enqueue(item) {
        if (item === 'undefined') {
            throw Error('No Arguments');
        }
        let newNode = new Node();
        newNode.value = item;
        if (!this._count) {
            this._first = newNode;
            this._last = newNode;
        } else {
            this._last.next = newNode;
        }
        this._count++;
    }
    dequeue() {
        if (this._count <= 0) {
            throw Error("Empty Queue");
        }
        let item = this._first.value;

        this._first = this._first.next;
        this._count--;
        if (!this._count) {
            this._last = null;
        }
        return item;
    }

    isEmpty() {
        return this._count === 0;
    }

    size() {
        return this._count;
    }
}

module.exports = Queue;